﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Detai.Lib.Common;
using Detai.Admin.Models;
using Detai.Admin.Components;

namespace Detai.Admin.Controllers
{

	[HandleError]
    public class AccountController : Controller
	{


		public ActionResult LogOn()
		{
            if (Request.IsAuthenticated)
            {
                var temp = new BaseAuthentication();
                var currentUser = temp.CurrentUser;
                if (currentUser != null && (currentUser.LoaiTK == 1 || currentUser.LoaiTK == 2))
                    return RedirectToAction("Index", "Home");
                else
                {
                    return RedirectToAction("LogOff");
                }

            }
            else return View();
		}

		[HttpPost]
		public ActionResult LogOn(LogOnModel model, string returnUrl)
		{
            try
            {
                DetaiDataContext eDataContext = new DetaiDataContext();
                var currentUser = eDataContext.TaiKhoans.Where(e => e.Tendangnhap == model.UserName && e.Matkhau == model.Password).FirstOrDefault();
                if (currentUser != null)
                {
                        var temp = new BaseAuthentication();
                        temp.Login(currentUser.Tendangnhap);
                        return RedirectToAction ("Index","Home");
                }
                else
                {
                    ViewData[Messages.MessageInfo]="Thông báo: Tên đăng nhập \"" + model.UserName + "\" hoặc mật khẩu không tồn tại trong hệ thống";

                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
		}

		public ActionResult LogOff()
		{
                var temp = new BaseAuthentication();
                temp.Logout();
                return RedirectToAction("Index", "Home");
		}

		// **************************************
		// URL: /Account/Register
		// **************************************
        [Authorize]
		public ActionResult Register()
		{
            //ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            //if (!UserManager.CheckRole(RoleSystem.Register))
            //{
            //    ViewData["MessageInfo"] = Messages.NOT_PERMIT;
            //}
			return View();
		}

		[HttpPost]
		public ActionResult Register(RegisterModel model)
		{
            //if (ModelState.IsValid)
            //{
            //    // Attempt to register the user
            //    MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

            //    if (createStatus == MembershipCreateStatus.Success)
            //    {
            //        //FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
            //        return RedirectToAction("Index", "UserAdministration");
            //    }
            //    else
            //    {
            //        ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
            //    }
            //}

            //// If we got this far, something failed, redisplay form
            //ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
			return View(model);
		}

		// **************************************
		// URL: /Account/ChangePassword
		// **************************************

        //[Authorize]
        //public ActionResult ChangePassword()
        //{
        //    ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        //    return View();
        //}

        //[Authorize]
        //[HttpPost]
        //public ActionResult ChangePassword(ChangePasswordModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
        //        {
        //            return RedirectToAction("ChangePasswordSuccess");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        //    return View(model);
        //}

		// **************************************
		// URL: /Account/ChangePasswordSuccess
		// **************************************

		public ActionResult ChangePasswordSuccess()
		{
			return View();
		}

	}
}
